package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;


    @RequestMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(id);
        ShoppingCart shoppingCartNew = shoppingCartService.addCart(shoppingCart);
        return R.success(shoppingCartNew);
    }


    @RequestMapping("/sub")
    public R<ShoppingCart> subCart(@RequestBody ShoppingCart shoppingCart){
     return  shoppingCartService.subCart(shoppingCart);
    }

@RequestMapping("/list")
    public R<List<ShoppingCart>> findAllByUserId(HttpSession session) {
    Long userId = (Long) session.getAttribute("user");
    List<ShoppingCart> shoppingCartList = shoppingCartService.findAllByUserId(userId);
    return R.success(shoppingCartList);
    }


@RequestMapping("/clean")
    public R<String> clean(HttpSession session) {
    Long userId = (Long) session.getAttribute("user");
    shoppingCartService.clean(userId);
    return R.success("清空购物车成功");
    }



}
